package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SysUser;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

	@Reference
	private UserService userService;
    @Reference
	RoleService roleService;
	@RequestMapping("/getUsername")
	public Result getUsername(){
		log.debug("获取用户名!!");
		//获取安全框架上下文对象
		SecurityContext context = SecurityContextHolder.getContext();
		//获取认证对象
		Authentication authentication = context.getAuthentication();
		//获取 Principal 重要对象
		Object principal = authentication.getPrincipal();
		if(principal instanceof User){
			User user = (User) principal;
			String username = user.getUsername();
			return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
		}
		return new Result(false,MessageConst.GET_USERNAME_FAIL);
	}

	@RequestMapping("/loginSuccess")
	public ModelAndView loginSuccess(){
		return new ModelAndView("redirect:http://oms.web.health:83/pages/main.html");
	}

	@RequestMapping("/loginError")
	public ModelAndView doError(){
		return new ModelAndView("redirect:http://oms.web.health:83/login.html");
	}

	@RequestMapping("/findPage")
	public Result findPage(@RequestBody QueryPageBean queryPageBean){
		log.debug("开始查询用户列表*******************");
		PageResult pageResult=userService.findPage(queryPageBean);
         log.debug("查询用户列表成功*************");

		return new Result(true,"获取用户列表成功",pageResult);
	}
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("controller -- findById: {"+id+"}");
        SysUser sysUser = userService.finById(id);
        log.debug("检查项根据id查询：{"+ sysUser +"}");
        return  new Result(true, "查询用户成功",sysUser);

    }
	@RequestMapping("/findRoleIdsByUserId")
	public Result findRoleIdsByUserId(Integer userId){
		List<Integer> list = roleService.findRoleIdsByUserId(userId);

		log.debug(list.toString()+"-----------------------");
		return new Result(true, "查询所有角色成功", list);
	}
	@RequestMapping("/edit")
	public Result edit(Integer[] roleIds,@RequestBody SysUser user){

            userService.edit(roleIds,user);

		return new Result(true, "修改用户信息成功");
	}
	@RequestMapping("/add")
	public Result add(Integer[] roleIds,@RequestBody SysUser user){
        log.debug(roleIds.toString()+"*****************************");
		userService.add(roleIds,user);

		return new Result(true, "新增用户信息成功");
	}
	@RequestMapping("/delById")
	public Result delById(Integer id){

		userService.delById(id);

		return new Result(true, "删除用户信息成功");
	}
	@RequestMapping("/findUsername")
	public Result findUsername(String username){
		log.debug(username+"得到用户名*****************************");


		return new Result(true, "成功!!");
	}

}